package br.dimap.ppgsc.fmapn.aspects;

import java.util.Date;
import java.util.Hashtable;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.dimap.ppgsc.fmapn.util.GerenciadorLookup;
import br.fmapn.analyzer.FacadeBusinessProcessFrameworkRemote;
import br.fmapn.exceptions.UserNotDefinedException;
import br.fmapn.process.entities.BusinessOperation;
import br.fmapn.process.entities.User;
import br.fmapn.process.entities.UserBusinessOperation;

/*
import br.fmapn.analyzer.FacadeBusinessProcessFrameworkRemote;
import br.fmapn.exceptions.UserNotDefinedException;
import br.fmapn.process.entities.BusinessOperation;
import br.fmapn.process.entities.User;
import br.fmapn.process.entities.UserBusinessOperation;
*/
public abstract aspect MonitorBusinessOperation {
	/*####################################################*/
	/**
	 * 
	 */
	/*####################################################*/
	private Map hashRequestPerUserThread = new Hashtable();
	/*####################################################*/
	/**
	 * 
	 */
	/*####################################################*/
	private FacadeBusinessProcessFrameworkRemote facadeBusinessProcessFW = GerenciadorLookup.obterFacadeBusinessProcessFramework();
	/*####################################################*/
	/**
	 * 
	 */
	/*####################################################*/
	public abstract pointcut webActionExecution(HttpServletRequest request, HttpServletResponse response);
	/*####################################################*/
	/**
	 * 
	 */
	/*####################################################*/
	before(HttpServletRequest request, HttpServletResponse response): webActionExecution(request,response){
		System.out.println("[Aspect] WebActionExecution Before Advice!");
		String threadId = Long.toString(Thread.currentThread().getId());
		//System.out.println("nome : " + request.getUserPrincipal().getName());
        hashRequestPerUserThread.put(threadId, request);
	}
	/*####################################################*/
	/**
	 * 
	 */
	/*####################################################*/
	public abstract pointcut businessOperationCalls();
	/*####################################################*/
	/**
	 * 
	 */
	/*####################################################*/
	before(): businessOperationCalls(){
		
		String threadId = Long.toString(Thread.currentThread().getId());
		HttpServletRequest request = (HttpServletRequest) hashRequestPerUserThread.get(threadId);
		
        String userBPName = "Meu Nome"; //request.getUserPrincipal().getName();
        
        User userBP = new User();
        userBP.setName(userBPName);
        BusinessOperation bOp = new BusinessOperation();
        String nameBP = thisJoinPoint.getSignature().toString();
        bOp.setName(nameBP);
        System.out.println("id: " + request.getSession().getId());
        String sessionBP = request.getSession().getId(); 
        Date occurrenceDate = new Date();
        
        System.out.println("[Aspect] BP User: " + userBP);
        System.out.println("[Aspect] BP Name: " + nameBP);
        System.out.println("[Aspect] BP Session ID: " + sessionBP);                
        System.out.println("[Aspect] BP Date: " + occurrenceDate);
        
        UserBusinessOperation bo = new UserBusinessOperation();
        
        bo.setUser(userBP);
        bo.setSession(sessionBP);
        bo.setBusinessOperation(bOp);
        //bo.setDate(occurrenceDate);       
        
        System.out.println("oi");
        try {
        	this.facadeBusinessProcessFW.registerBusiness(bo);
		} catch (UserNotDefinedException e) {			
			e.printStackTrace();
		}
	}
	
}